<?php

namespace App\Http\Controllers\Common;

use App\Model\Common;
use App\Model\Spread;
use App\Model\WeChat;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Symfony\Component\Routing\Route;

class TestController extends Controller
{
    // 上传微信素材
    public function uploadMedia(Request $request)
    {
        // 测试服务器 nm3xLzV731jHEWOPHJbp3SXjqLlo5IeJwzLOY4hNxsqPpjvyS3SVe7FSfF-sMobe
        // 正式服务器
        $url = public_path('/data/upload/2018/11/09/eRUSHjnPYd5jWPk1.jpg');
        $weChat = new WeChat();
        $media_id = $weChat->uploadWeChatImg($url);

        return $this->renderJson($media_id);
    }

    public function testSpread()
    {

        $spread = new Spread();
//        while(1){
            $data = $spread
                ->where("maibao_num_surplus", '=',0)
                ->where("is_deleted", "=", 0)
                ->where("status",'=',2)
                ->pluck("id");
            if (count($data) > 0 ){
                echo "正在执行！";
                $spread->whereIn("id",$data)->update(["status"=>3]);
                try{
                    \DB::beginTransaction();
                    $res =  $spread->whereIn("id",$data)->update(["status"=>3]);
                    var_dump($res);
                    if ($res < 0){
                        \DB::rollBack();
                        throw  new  \Exception("修改失败");
                    }
                    \DB::commit();
                }catch (\Exception $e ){
                    \Log::error($e);
                }
            }else{
                echo "10后在执行！";
                sleep(10);
            }
//        }

    }

    public function testImg()
    {
        $path = "http://thirdwx.qlogo.cn/mmopen/vi_32/Uz0EHlJnP5BCCR6LsScX3KoIiatBAVjLT8R2DiaUIFqcwGU7ibiaTENa208hImwVlPZISX8NkyPStHYWLoD2YYr6ng/132";
        $url = Common::curl_file_get_contents ($path);

        $imgName = \Illuminate\Support\Str::random(16) . time() . '.jpg';
        $imgPath = base_path('public') . '/' . $imgName;
        file_put_contents ($imgPath, $url);
        $re = $this->imgToBase64($imgPath);
        unlink(public_path($imgName));
        return $re;
    }

    function imgToBase64($img_file) {

        $img_base64 = '';
        if (file_exists($img_file)) {
            $app_img_file = $img_file; // 图片路径
            $img_info = getimagesize($app_img_file); // 取得图片的大小，类型等

            //echo '<pre>' . print_r($img_info, true) . '</pre><br>';
            $fp = fopen($app_img_file, "r"); // 图片是否可读权限

            if ($fp) {
                $filesize = filesize($app_img_file);
                $content = fread($fp, $filesize);
                $file_content = chunk_split(base64_encode($content)); // base64编码
                switch ($img_info[2]) {           //判读图片类型
                    case 1: $img_type = "gif";
                        break;
                    case 2: $img_type = "jpg";
                        break;
                    case 3: $img_type = "png";
                        break;
                }

                $img_base64 = 'data:image/' . $img_type . ';base64,' . $file_content;//合成图片的base64编码

            }
            fclose($fp);
        }

        return $img_base64; //返回图片的base64
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function simulate ( Request $request )
    {

        $user = User::wherePhone ( $request->input ( "phone" ) )->whereIn ( 'type', [3, 4] )->first ();
        if ($user == null) {
            return $this->fail ( "用户名不存在！" );
        }

        if ($user->is_enabled == 1) {
            return $this->fail ( '你的账号已被禁用' );
        }
        $is_perfect = isset($user->cards) ? true :false;
        //登录成功记录数据
        return $this->renderJson ( ['token' => \Auth::guard ( 'app' )->fromUser ( $user ), 'expire' => '2592000', 'is_perfect' => $is_perfect] );
    }

}
